import request from './request'

/**
 * 数据源管理API
 */

// 获取数据源列表
export function getDataSources(params) {
  return request({
    url: '/data-sources/',
    method: 'get',
    params
  })
}

// 获取数据源详情
export function getDataSource(id) {
  return request({
    url: `/data-sources/${id}`,
    method: 'get'
  })
}

// 创建数据源
export function createDataSource(data) {
  return request({
    url: '/data-sources/',
    method: 'post',
    data
  })
}

// 更新数据源
export function updateDataSource(id, data) {
  return request({
    url: `/data-sources/${id}`,
    method: 'put',
    data
  })
}

// 删除数据源
export function deleteDataSource(id) {
  return request({
    url: `/data-sources/${id}`,
    method: 'delete'
  })
}

// 测试数据源连接
export function testDataSource(data) {
  return request({
    url: '/data-sources/test-connection',
    method: 'post',
    data
  })
}

// 预览数据源数据
export function previewDataSource(id, params) {
  return request({
    url: `/data-sources/${id}/preview`,
    method: 'get',
    params
  })
}

// 上传数据文件
export function uploadDataFile(file) {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/data-sources/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
